package com.subway.babynote;

/*水*/
import com.subway.babynote.syncdata.ClientDataRecord;
import com.subway.babynote.syncdata.ClientDataRecord.usersRecord;
import com.subway.babynote.syncdata.StringUtil;
import com.subway.babynote.syncdata.errMessage;
import com.subway.babynote.syncservice.syncapi;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class register extends Activity implements OnClickListener {
	private Button register_button;
	EditText username_et;
	EditText password_et;
	EditText passwordrepeat_et;
	TextView hint_view;
	Intent In;
	private LinearLayout linearLayout, statistics, sync, more;
	TextView log_view;
	NotedbHelper notedb;
	int col;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		In = new Intent();
		notedb = new NotedbHelper(this);
	//	initTabFunction();
		register_button = (Button) findViewById(R.id.register_button);
		username_et = (EditText) findViewById(R.id.username_edit);
		password_et = (EditText) findViewById(R.id.passwd_edit);
		passwordrepeat_et = (EditText) findViewById(R.id.passwdrepeat_edit);
		hint_view = (TextView) findViewById(R.id.hint_view);
		log_view = (TextView) findViewById(R.id.log_view);
		register_button.setOnClickListener(this);
		log_view.setOnClickListener(this);
	}

	public boolean checkPassword() {
		if (password_et.getText().toString()
				.equals(passwordrepeat_et.getText().toString())) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.equals(register_button)) {
			hint_view.setText("");
			if (!checkPassword()) {
				hint_view.setText(getResources().getString(R.string.reenterpasswd));
				return;
			}
			try {
				ClientDataRecord.usersRecord ur = new usersRecord();
				Cursor cur = notedb.getVisitor();
				this.startManagingCursor(cur);
				if (cur != null && cur.getCount() > 0) {
					col = cur.getColumnIndex("birth");
					ur.birth = cur.getInt(col);
					col = cur.getColumnIndex("gender");
					if (cur.getInt(col) == 0) {
						ur.gender = true;
					} else {
						ur.gender = false;
					}
					col = cur.getColumnIndex("first_use_time");
					ur.first_use_time = cur.getInt(col);
				} else {
					ur.gender = true;
					ur.first_use_time =notedb.nowTime();
				}

				ur.first_reg_time = notedb.nowTime();
				ur.ts = notedb.nowTime();
				ur.email = username_et.getText().toString();
				ur.passwd = password_et.getText().toString();
				hint_view.setText(getResources().getString(R.string.registering));
				int ret = syncapi.getSyncImpl().RegUser(ur);

				if (ret == errMessage.err_mailexist.errcode) {
					hint_view.setText(getResources().getString(R.string.emailexists));
					return;
				} else {
					hint_view.setText(getResources().getString(R.string.loginsuccess));
					In.setClass(register.this, feedcategory.class);
					startActivity(In);
				}

			} catch (Exception e) {
				Log.v("register", "get info from DB e=" + e.getMessage());
			}

			In.setClass(register.this, maintab.class);
			startActivity(In);
		} else if (v.equals(log_view)) {
			In.setClass(register.this, login.class);
			startActivity(In);

		}
	}



	public void onDestroy() {
		notedb.close();
		super.onDestroy();

	}
}